package day12;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

/**
 * @author oldliu
 * @since 1.0
 */
public class AdvanceTalkSever {
    public static void main(String[] args) {
        try {
            ServerSocket st=new ServerSocket(9996);
            while (true){
                //new TalkThread(st.accept()).start();
                // new LoginThread(st.accept()).start();
                Thread t=new SendFileThread(st.accept());
                t.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
class TalkThread extends Thread{
    private final Socket s;
    public TalkThread(Socket s){
        this.s=s;
    }
    @Override
    public void run() {
        System.out.println(s+"链接了");
        try {
            InputStream in = s.getInputStream();
            OutputStream out = s.getOutputStream();
            //构建的字符流，为了聊天
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            PrintWriter pw = new PrintWriter(out, true);
            String ask = null;
            while (true) {
                ask = br.readLine();
                System.out.println("问：" + ask);
                pw.println("你的问题【" + ask + "】我收到了，稍后联系你");
                //>>>pw.flush();
                if ("byebye".equals(ask))
                    break;
            }
        } catch (IOException e) {
            System.out.println("出错了："+e);
            e.printStackTrace();
        } finally {
            try {
                s.close();
                System.out.println(s+"退出了。。。");
            } catch (Exception e) {
                //e.printStackTrace();
            }
        }
    }
}

class LoginThread extends Thread{
    private final Socket s;
    static Map<String,String> users=new HashMap<>();
    static{
        users.put("zhangchangxiong","1234");
        users.put("liuguihen","pwd");
        users.put("kangkai","123456");
    }
    public LoginThread(Socket s){
        this.s=s;
    }
    @Override
    public void run() {
        System.out.println(s+"链接了");
        try {
            InputStream in = s.getInputStream();
            OutputStream out = s.getOutputStream();
            //构建的字符流，为了聊天
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            PrintWriter pw = new PrintWriter(out, true);
            String ask = null;
            while (true) {
                String user = br.readLine();
                String pass = br.readLine();
                if(user==null||user.equals("byebye"))
                    break;
                if (pass.equals(users.get(user)))
                    pw.println("登录成功");
                else
                    pw.println("错误的用户名或者密码！");
            }
        } catch (IOException e) {
            System.out.println("出错了："+e);
            e.printStackTrace();
        } finally {
            try {
                s.close();
                System.out.println(s+"退出了。。。");
            } catch (Exception e) {
                //e.printStackTrace();
            }
        }
    }
}

class SendFileThread extends Thread{
    private final Socket s;

    //
    public SendFileThread(Socket s){
        this.s=s;
    }

    @Override
    public void run() {
        System.out.println(s+"链接了");
        try {
            //读文件
            FileInputStream fin=new FileInputStream("D:\\Documents\\图片和资料\\job-salary.png");
            byte []b=new byte[fin.available()];
            int len=fin.read(b);
            fin.close();
            OutputStream out = s.getOutputStream();
            out.write(b,0,len);//len=b.length
            out.flush();
            System.out.println("文件发送给"+s+"成功了");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                s.close();
            } catch (Exception e) {

            }
        }

    }
}